package cn.lutu.adapter.controller;

import cn.lutu.domain.service.UserService;
import cn.lutu.protocol.dto.*;
import cn.lutu.protocol.vo.UserLoginResultVO;
import cn.lutu.protocol.vo.UserVO;
import cn.lutu.util.response.ResultVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


/**
 * @date 2023/6/5
 * @author songjie
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("/v1/users/")
@Slf4j
public class UserController {
    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    UserService userService;
    /**
     * 注册
     * */
    @PostMapping("/reg")
    @ApiOperation( "用户注册")
    public ResultVO reg(UserRegDTO userRegDTO){

       log.info("进入注册请求");
       userService.addNew(userRegDTO);
        return ResultVO.ok();
    }
    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 10)
    public ResultVO login(@Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpServletRequest request) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginDTO);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader("user-agent");
        UserLoginResultVO login = userService.login(userLoginDTO,remoteAddr,userAgent);
        return ResultVO.ok(login);
    }
    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    @ApiOperationSupport(order = 11)
    public ResultVO logout() {
        log.debug("开始处理【用户退出登录】的请求，无参数");
        SecurityContextHolder.clearContext();
        return ResultVO.ok();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息")
    public ResultVO update(UserUpdateDTO userUpdateDTO,Long id){
        log.info("开始处理修改用户信息请求");
        userService.updateInfoById(userUpdateDTO,id);
        return ResultVO.ok();
    }

    @ApiOperation("根据ID查询用户")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    @GetMapping("/details/{id:[0-9]+}")
    public ResultVO selectById(@PathVariable @Range(min = 1, message = "获取用户详情失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询用户】的请求，参数：{}", id);
        UserVO userVO = userService.selectById(id);
        return ResultVO.ok(userVO);
    }

    @PostMapping("retrievePassword")
    @ApiOperation(value="找回密码")
    public ResultVO retrievePassword(@RequestBody RetrievePasswordDTO userChangePasswordDTO){
        userService.retrievePasswordUser(userChangePasswordDTO);
        return ResultVO.ok();
    }
    @PostMapping("changePassword")
    @ApiOperation(value="修改密码")
    public ResultVO changePassword(UserChangePasswordDTO userChangePasswordDTO,Long id){
        System.out.println("userChangePasswordDTO = " + userChangePasswordDTO + ", id = " + id);
       userService.changePasswordUser(userChangePasswordDTO, id);
       return ResultVO.ok();
    }
}
